package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_find_second_hot_recommend.SecondHotRecommendAlbum;
import com.west_traveler.utils.GetCirclePhoto;
import com.west_traveler.ximalayafm.R;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Description:
 * created at 2016/12/7 11:24
 *
 * @author: 黄昌友
 */

public class SecondAlbumRecycleViewAdapter extends RecyclerView.Adapter<SecondAlbumRecycleViewAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private SecondHotRecommendAlbum.DataBean.TracksBean track;
    private List<SecondHotRecommendAlbum.DataBean.TracksBean.ListBean> data;
    private RecyclerView recyclerView;
    private OnChildClickListener listener;

    public void setListener(OnChildClickListener listener) {
        this.listener = listener;
    }

    public SecondAlbumRecycleViewAdapter(Context context, SecondHotRecommendAlbum.DataBean.TracksBean track) {
        this.context = context;
        this.track = track;
        this.data = track.getList();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.second_album_recycle_item, parent, false);
        view.setOnClickListener(this);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv_play_title.setText(data.get(position).getTitle());
        if (data.get(position).getPlaytimes() / 10000 > 0) {
            holder.tv_play_count_value.setText(data.get(position).getPlaytimes() / 10000 + "万次");
        } else {
            holder.tv_play_count_value.setText(data.get(position).getPlaytimes() + "次");
        }
        holder.tv_comment_value.setText(data.get(position).getComments() + "");
        holder.tv_update.setText(new SimpleDateFormat("yyyy-MM").format(new Date(data.get(position).getCreatedAt())));
        final Bitmap[] bitmap = {null};

        new Thread() {
            @Override
            public void run() {
                try {
                    if (data.get(position).getCoverSmall() == null && !"".equals(data.get(position).getCoverSmall())) {
                        bitmap[0] = Picasso.with(context).load(data.get(position).getCoverSmall()).get();
                        final Bitmap transform = new GetCirclePhoto().transform(bitmap[0]);
                        holder.iv_album_icon.post(new Runnable() {
                            @Override
                            public void run() {
                                holder.iv_album_icon.setImageBitmap(transform);
                            }
                        });
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public void onClick(View view) {
        if (recyclerView != null && listener != null) {
            int position = recyclerView.getChildAdapterPosition(view);
            long id = getItemId(position);
            listener.onChildClick(position, id, track);
        }
    }

    public interface OnChildClickListener {
        void onChildClick(int position, long id, SecondHotRecommendAlbum.DataBean.TracksBean data);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_aleary_play_value;
        private ImageView iv_download_icon;
        private TextView tv_comment_value;
        private TextView tv_play_time_value;
        private TextView tv_play_count_value;
        private TextView tv_update;
        private TextView tv_play_title;
        private ImageView iv_album_icon;
        private RelativeLayout rl_album_group;

        public ViewHolder(View itemView) {
            super(itemView);

            iv_album_icon = (ImageView) itemView.findViewById(R.id.iv_album_icon);
            iv_download_icon = (ImageView) itemView.findViewById(R.id.iv_download_icon);

            tv_play_title = ((TextView) itemView.findViewById(R.id.tv_play_title));
            tv_update = ((TextView) itemView.findViewById(R.id.tv_update));
            tv_play_count_value = ((TextView) itemView.findViewById(R.id.tv_play_count_value));
            tv_play_time_value = ((TextView) itemView.findViewById(R.id.tv_play_time_value));
            tv_comment_value = ((TextView) itemView.findViewById(R.id.tv_comment_value));
            tv_aleary_play_value = ((TextView) itemView.findViewById(R.id.tv_aleary_play_value));

            rl_album_group = ((RelativeLayout) itemView.findViewById(R.id.rl_album_group));
        }
    }
}
